<template>
  <div class="ChildPage TablePage" v-loading="loading">
    <vb-page ref="page" @onTableSelection="doSelect">
      <template #headerSearch-btn-append="{ searchForm }">
        <el-button size="small" icon="el-icon-plus" type="primary" @click="syncPriceList()">同步价目表</el-button>
      </template>


      <template #table-handle-prepend="scope">
        <el-button size="mini" type="text" icon="el-icon-edit" @click="gotoDetail(scope.row.billNo)">查看</el-button>
      </template>
      <template #table-isUpload="scope">
        <el-tag :type="scope && scope.row && scope.row.isUpload === 'yes'? 'success': 'info'">{{scope && scope.row && scope.row.isUpload === 'yes'? '已上传': '未上传'}}</el-tag>
      </template>
      <template #table-orderUpLoad="scope">
        <el-tag :type="scope && scope.row && scope.row.orderUpLoad === 'yes'? 'success': 'info'">{{scope && scope.row && scope.row.orderUpLoad === 'yes'? '已上传': '未上传'}}</el-tag>
      </template>

    </vb-page>
  </div>

</template>

<script>

export default {
  data() {
    return {
      loading:false,
      selection: null,
      selectionLength:0,
      stockLoading: false,
      reloadStock: false,
    }
  },
  components: {

  },
  methods: {
    async syncPriceList (){
      try {
        this.$message.warning('正在同步')
        let res = await this.callApi('sync_price_list',{})
        if(!res.res){
          return
        }
        this.$message.success('同步完成,请查看')
        this.$refs.page.reload()
      }finally {
        this.loading = false
      }
    },
    async getStockReloadResult(showMsg) {
      this.stockLoading = true
      try {
        let res = await this.callApi("get_stock_reload_result")
        if (!res.res) return
        this.reloadStock = res.reloadStock
        this.reloadStockRes = res.reloadStockRes
        this.reloadStockMsg = res.reloadStockMsg
        this.reloadStockStartTime = res.reloadStockStartTime
        this.reloadStockEndTime = res.reloadStockEndTime
        if (showMsg) {
          if (this.reloadStock) {
            this.$message.info('数据正在上传中')
          } else {
            if (this.reloadStockRes) {
              this.reload()
              this.$message.success('已同步完成, 请刷新最新结果')
            } else {
              this.$message.error('同步失败，' + this.reloadStockMsg)
            }
          }
        }
      } finally {
        this.stockLoading = false
      }
    },

    async uploadKingdee(billNoList){
      if (!this.selection || this.selection === 0){
        this.$message.error('没有选择行')
        return
      }
      const arrBillNo = [];
      for (let i = 0;i<billNoList.length;i++){
        arrBillNo.push(billNoList[i].tradeNo)
      }

      try {
        this.$message.warning('正在上传')
        let res = await this.callApi('upload_kingdee_sale',{billNoList:arrBillNo})
        if(!res.res){
          return
        }
        this.$message.success('上传成功')
        this.$refs.page.reload()
      }finally {
        this.loading = false
      }
    },
    doSelect(data) {
      this.selection = data
      this.selectionLength = this.selection.length;
      this.selectCount=0
      for (var i=0;i<this.selectionLength;i++){
        this.selectCount+=data[i].count
      }
      this.selectCount = parseFloat(this.selectCount).toFixed(2)
      console.log(this.selection)
    }
  }
}
</script>

<style scoped>

</style>
